package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.cache.Cache;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.model.Activity;
import com.stylefeng.guns.common.persistence.model.StuAct;
import com.stylefeng.guns.common.persistence.model.Student;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.biz.service.ActService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/act")
public class ActivityController extends BaseController {

    private static String PREFIX = "/biz/activity";

    @Resource
    ActService actService;

    /**
     * 跳转到活动列表页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/activities.html";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String actName, @RequestParam(required = false) String actYear){
        Map<String,Object> params=new HashMap();
        params.put("acName",actName);
        params.put("acYear",actYear);
        List<Activity> list=actService.selectActList(params);
        return list;
    }

    /**
     * 跳转到添加活动
     */
    @RequestMapping(value = "/add")
    public String actAdd() {
        return PREFIX + "/add.html";
    }

    @RequestMapping(value = "/add_act")
    @BussinessLog(value = "添加活动", key = "name")
    @ResponseBody
    public Tip add(@Valid Activity activity, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        activity.setAcid(null);
        this.actService.insert(activity);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到修改活动
     */
    @RequestMapping(value = "/edit/{acid}/{type}")
    public String toEdit(@PathVariable Integer acid, @PathVariable Integer type, Model model) {
        if (ToolUtil.isEmpty(acid)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Activity activity=actService.selectActById(acid);
        model.addAttribute("act",activity);

        LogObjectHolder.me().set(activity);
        if(type==1)
            return PREFIX + "/edit.html";
        else{
            return PREFIX + "/view.html";
        }
    }

    /**
     * 活动修改
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Tip edit(@Valid Activity activity,BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.actService.update(activity);

        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return SUCCESS_TIP;
    }
}

